package demo.IOInputOutput;

import java.io.*;
import java.util.Arrays;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/11-08-11-14:55
 * @Description：demo.IOInputOutput  字节数组输入输出流
 */
public class ArrOutPutStream {
    public static void main(String[] args) {
        //1、声明创建流
        //数据从字节数组中取
        byte[] arr = "hello".getBytes();
        InputStream in = new ByteArrayInputStream(arr);
        //写到字节数组中
        OutputStream out = new ByteArrayOutputStream();    //默认是32的大小
        try {
            //2、使用流
            byte[] b = new byte[2];     //缓冲数组
            int len;
            while((len = in.read(b))!=-1){
                //写数据，将数据写到字节数组中    写入进了内部的buf[]数组中
                out.write(b,0,len);
            }
            //从内部取出buf[]数组中
            byte[] copy = ((ByteArrayOutputStream)out).toByteArray();
            System.out.println(new String(copy));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //3、关闭流
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
